package com.fdb.efp.esb.service.impl.sed.customer;

import com.alibaba.fastjson.JSON;
import com.fdb.efp.esb.common.client.resp.EsbRespRetInfBean;
import com.fdb.efp.esb.common.client.resp.EsbRespServiceBean;
import com.fdb.efp.esb.common.client.resp.EsbRespSysHeadBean;
import com.fdb.efp.esb.common.constant.ESBClientConstance;
import com.fdb.basic.framework.core.constant.ESBConstant;
import com.fdb.basic.framework.core.constant.EsbRetCodeStandard;
import com.fdb.basic.framework.core.exception.ESBException;
import com.fdb.efp.esb.service.bo.req.sed.customer.ReqSedCusToOnlineFinanceBean;
import com.fdb.efp.esb.service.bo.resp.sed.customer.RespSedCusToOnlineFinanceBean;
import com.fdb.efp.esb.service.client.ESBRequestBuilder;
import com.fdb.efp.esb.service.client.EsbBaseBean;
import com.fdb.efp.esb.service.facade.sed.customer.CustomerCollectionService;

import java.util.Objects;
import java.util.Optional;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

/**
* 普税贷产品对税e贷产品的此部分代码做了复用
* 说明：为了保证功能的复用性、提升开发的工作效率，对该代码进行了复用
* 评估是否会影响线上功能：否
* 评估时间：2020/5/25
* 处理方案：保留复用
*/
/**
 * 需求编号:2019D0519
 * 问题编号:
 * 开发人员: caoxin
 * 创建日期:2019/7/17
 * 功能描述:客人信息维护 impl
 * 修改日期:2019/7/17
 * 修改描述:
 */
@Service("customerCollectionService")
public class CustomerCollectionServiceImpl implements CustomerCollectionService {
    /**
     * TODO: 客户信息补录<br>
     * <p>
     * 0.1:caoxin:2019/4/19 10:23: TODO: <br>
     *
     * @author caoxin
     * @return
     * @date 2019/4/19 10:23
     * @version 0.1
     * @since 2019/4/19 10:23
     */
    private static Logger logger = LoggerFactory.getLogger(CustomerCollectionServiceImpl.class);

    @Override
    public RespSedCusToOnlineFinanceBean pushCustomerInfoToOnlineFinance(ReqSedCusToOnlineFinanceBean reqSedCustomerCollectionBean,String gloNo) throws ESBException {

        logger.info("获取客户维护信息，参数为：" + JSON.toJSONString(reqSedCustomerCollectionBean));
        RespSedCusToOnlineFinanceBean respSedCusToOnlineFinanceBean = null;

/*      EsbRespServiceBean esbRespServiceBean = ESBClientFactory.
                buildClient().
                withTradeNo(ESBConstant.TRADE_CODE_20120001).
                withScene(ESBConstant.SCENE_NO_07).
                withBody(reqSedCustomerCollectionBean).
                create().
                sendAndReceive();*/

        EsbBaseBean esbBaseBean = EsbBaseBean.EsbBaseBeanBuilder.
                anEsbBaseBean().
                withINSTID(ESBClientConstance.HPJRBORGID).withGLBLSRLNO(gloNo).build();
        EsbRespServiceBean esbRespServiceBean = ESBRequestBuilder.buildClient().
                withEsbBaseBean(esbBaseBean)
                .withBody(reqSedCustomerCollectionBean)
                .withTradeNo(ESBConstant.TRADE_CODE_20120001)
                .withScene(ESBConstant.SCENE_NO_07)
                .create()
                .sendAndReceive();

        String retCode = "";
        // 通过系统头判断是否成功系统头
        EsbRespSysHeadBean sysHead =
                esbRespServiceBean.getSysHead();
        if (Objects.nonNull(sysHead)) {
            // 返回信息数据不为空
            EsbRespRetInfBean esbRespRetInfBean = sysHead.getRetInfArry().stream().findFirst().orElseGet(EsbRespRetInfBean::new);
            retCode = Optional.ofNullable(esbRespRetInfBean.getRetCd()).orElse(StringUtils.EMPTY);
            if (ESBConstant.SUCCESS.equals(retCode)) {
                String msg = "调用客户信息维护接口成功";
                logger.debug(msg);
                respSedCusToOnlineFinanceBean = esbRespServiceBean.getBody(RespSedCusToOnlineFinanceBean.class);
            } else {
                String msg = "调用客户信息维护接口调用失败 " + esbRespRetInfBean.getRetMsg();
                logger.error(msg);
                throw new ESBException(EsbRetCodeStandard.OTHERS_EXCEPTION.getValue(),msg);
            }
        }
        return respSedCusToOnlineFinanceBean;
    }
}
